﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BackgrounderService;

namespace LiveMonitor
{
    public class LiveMonInfo
    {
        public int defaultMaxMemoryLimit = 80;
        public int maxMemoryLimit;
        public bool enabled = true;
    }

    public class LiveMonitor : BackgrounderService.IPlugin
    {

        KeyValuePair<string, SettingHandler>? IPlugin.GetSettingCallBack()
        {
            KeyValuePair<string, SettingHandler> p = new KeyValuePair<string, SettingHandler>("LiveMonitor", this.Setting);
            return p;
        }

        public void Setting()
        {
            //System.Windows.Forms.MessageBox.Show("setting");
            try
            {
                var Dialog = new MonitorSetting(liveMonInfo);
                Dialog.SetData();
                Dialog.ShowDialog();
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        void IPlugin.Initialization(HookProxy hp)
        {
            P = new Properties.Settings();
            hp.RegisterSys(this.SysCall);
            running = false;
            memHaveWarned = false;
            liveMonInfo = new LiveMonInfo();
            liveMonInfo.maxMemoryLimit = P.MaxMemoryLimit;
        }

        //public void MouseCall(BackgrounderService.MouseEventArgs mouseEvent)
        //{
        //    if (running)
        //    {
        //        lock (liveMonLock)
        //        {
                    
        //        }
        //    }
        //}

        //public void KeyCall(BackgrounderService.KeyEventArgs keyEvent)
        //{
        //    if (running)
        //    {
        //        lock (liveMonLock)
        //        {

        //        }
        //    }
        //}

        public void SysCall(BackgrounderService.SysPerfArgs sysEvent)
        {
            //System.Windows.Forms.MessageBox.Show(sysEvent.memory.ToString());
            if (running)
            {
                lock (liveMonLock)
                {
                    if ((sysEvent.memory >= liveMonInfo.maxMemoryLimit) && (!memHaveWarned))
                    {
                        System.Windows.Forms.MessageBox.Show("内存使用率过高！");
                        memHaveWarned = true;
                    }
                    else if (sysEvent.memory < liveMonInfo.maxMemoryLimit)
                    {
                        memHaveWarned = false;
                    }
                }
            }
        }

        string IPlugin.Name
        {
            get { return "LiveMonitor"; }
        }

        void IPlugin.Start()
        {
            //System.Windows.Forms.MessageBox.Show("start");
            running = true;
        }

        void IPlugin.Stop()
        {
            //System.Windows.Forms.MessageBox.Show("stop");
            running = false;
        }

        void IPlugin.Unload()
        {
            P.MaxMemoryLimit = liveMonInfo.maxMemoryLimit;
        }

        State IPlugin.state
        {
            get
            {
                if (running) return State.Start;
                else return State.Stop;
            }
        }


        private Properties.Settings P;
        LiveMonInfo liveMonInfo;
        bool running;
        bool memHaveWarned;
        private static object liveMonLock = new object();
    }
}
